﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Linq;
using TAXILISM.Class;

namespace TAXILISM
{
    public class SearchPlaceFunction
    {
        public int cnt_client;
        public void feedAuto(string searchFrom, string searchTo)
        {
            try
            {
                WebClient GoogleAutoComplete1 = new WebClient();
                WebClient GoogleAutoComplete2 = new WebClient();

                GoogleAutoComplete1.DownloadStringAsync(new Uri("https://maps.googleapis.com/maps/api/place/autocomplete/xml?input=" + searchFrom + "&sensor=true&location=13.764912,100.538303&radius=300000&key=AIzaSyD8MrUZToMrnI4RZggiWubLuC_pEbVktt8"));
                GoogleAutoComplete1.DownloadStringCompleted += new DownloadStringCompletedEventHandler(GoogleFromAutoComplete_DownloadStringCompleted);
                cnt_client++;
                GoogleAutoComplete2.DownloadStringAsync(new Uri("https://maps.googleapis.com/maps/api/place/autocomplete/xml?input=" + searchTo + "&sensor=true&location=13.764912,100.538303&radius=300000&key=AIzaSyD8MrUZToMrnI4RZggiWubLuC_pEbVktt8"));
                GoogleAutoComplete2.DownloadStringCompleted += new DownloadStringCompletedEventHandler(GoogleToAutoComplete_DownloadStringCompleted);
                cnt_client++;
            }
            catch
            {

            }
        }

        public void feedPlace(string searchFrom, string searchTo)
        {
            try
            {
                WebClient GoogleSearchPlace1 = new WebClient();
                WebClient GoogleSearchPlace2 = new WebClient();
                GoogleSearchPlace2.DownloadStringAsync(new Uri("https://maps.googleapis.com/maps/api/place/search/xml?location=13.764912,100.538303&name="+ searchTo+"&sensor=false&radius=300000&key=AIzaSyD8MrUZToMrnI4RZggiWubLuC_pEbVktt8"));
                GoogleSearchPlace2.DownloadStringCompleted += new DownloadStringCompletedEventHandler(GoogleToSearchPlace_DownloadStringCompleted);
                cnt_client++;
                GoogleSearchPlace1.DownloadStringAsync(new Uri("https://maps.googleapis.com/maps/api/place/search/xml?location=13.764912,100.538303&name="+ searchFrom +"&sensor=false&radius=300000&key=AIzaSyD8MrUZToMrnI4RZggiWubLuC_pEbVktt8"));
                GoogleSearchPlace1.DownloadStringCompleted += new DownloadStringCompletedEventHandler(GoogleFromSearchPlace_DownloadStringCompleted);
                cnt_client++;

            }
            catch
            {

            }
        }

        public List<googlePlace> from_list_place = new List<googlePlace>();
        public List<googlePlace> to_list_place = new List<googlePlace>();
        void GoogleFromAutoComplete_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                string a = e.Result;
                XElement elm = XElement.Parse(e.Result);

                XElement[] dataTerm = elm.Descendants("prediction").ToArray();

                from_list_place.Clear();
                //try{
                for (int countTerm = 0; countTerm < dataTerm.Count(); countTerm++)
                {
                    var placeAuto = from placeAutodb in dataTerm[countTerm].Descendants("term")
                                    select new googlePlace
                                    {
                                        Data_term = placeAutodb.Element("value").Value
                                    };

                    string sortPlace = null;
                    string key = null;
                    int cou = 0;
                    foreach (googlePlace rs in placeAuto)
                    {
                        if (cou == 0)
                        {
                            key = rs.Data_term.ToString();
                        }
                        if (cou < placeAuto.Count() - 1)
                        {
                            sortPlace += " " + rs.Data_term.ToString();
                            cou++;
                        }
                    }
                    googlePlace last = new googlePlace();
                    last.Data_description = sortPlace;
                    last.Data_term = key;
                    from_list_place.Add(last);
                }
                //}
                //catch
                //{
                //    MessageBox.Show("ไม่พบข้อมูลที่ต้องการหา");
                //}
                cnt_client--;
            }
            catch (WebException)
            {
                MessageBox.Show("Please check your internet connection.");
                return;
            }
            catch (Exception)
            {
                MessageBox.Show("Server connection failed, Please wait a minute and try again.");
                return;
            }
        }

        void GoogleToAutoComplete_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                string a = e.Result;
                XElement elm = XElement.Parse(e.Result);

                XElement[] dataTerm = elm.Descendants("prediction").ToArray();

                to_list_place.Clear();
                //try
                //{
                for (int countTerm = 0; countTerm < dataTerm.Count(); countTerm++)
                {
                    var placeAuto = from placeAutodb in dataTerm[countTerm].Descendants("term")
                                    select new googlePlace
                                    {
                                        Data_term = placeAutodb.Element("value").Value
                                    };
                    string sortPlace = null;
                    string key = null;
                    int cou = 0;
                    foreach (googlePlace rs in placeAuto)
                    {
                        if (cou == 0)
                        {
                            key = rs.Data_term.ToString();
                        }
                        if (cou < placeAuto.Count() - 1)
                        {
                            sortPlace += " " + rs.Data_term.ToString();
                            cou++;
                        }
                    }
                    googlePlace last = new googlePlace();
                    last.Data_description = sortPlace;
                    last.Data_term = key;
                    to_list_place.Add(last);
                }
                //}
                //catch {
                //    MessageBox.Show("ไม่พบข้อมูลที่ต้องการหา");
                //}
                cnt_client--;
            }
            catch (WebException)
            {
                MessageBox.Show("Please check your internet connection.");
                return;
            }
            catch (Exception)
            {
                MessageBox.Show("Server connection failed, Please wait a minute and try again.");
                return;
            }
        }


        public List<googleLatLong> from_list_point = new List<googleLatLong>();
        public List<googleLatLong> to_list_point = new List<googleLatLong>();
        void GoogleFromSearchPlace_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            from_list_point.Clear();
            XElement elm = XElement.Parse(e.Result);
            var searchP = from sp in elm.Descendants("location")
                          select new googleLatLong
                          {
                              Data_lat = sp.Element("lat").Value,
                              Data_long = sp.Element("lng").Value
                          };
            foreach (googleLatLong rs in searchP)
            {
                from_list_point.Add(rs);
            }
            cnt_client--;
        }

        void GoogleToSearchPlace_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            to_list_point.Clear();
            XElement elm = XElement.Parse(e.Result);
            var searchP = from sp in elm.Descendants("location")
                          select new googleLatLong
                          {
                              Data_lat = sp.Element("lat").Value,
                              Data_long = sp.Element("lng").Value
                          };
            foreach (googleLatLong rs in searchP)
            {
                to_list_point.Add(rs);
            }
            cnt_client--;
        }
    }
}
